package com.sh;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class UdpClient {
    public static void main(String[] args) {
        // 创建一个 UDP 套接字
        try (DatagramSocket socket = new DatagramSocket();
             Scanner scanner = new Scanner(System.in)) {

            System.out.println("客户端启动，输入消息并按回车发送，输入 'exit' 退出：");

            while (true) {
                String message = scanner.nextLine();

                if ("exit".equalsIgnoreCase(message)) {
                    System.out.println("客户端退出");
                    break;
                }

                byte[] data = message.getBytes();
                // 创建数据包，包含数据、目标 IP 地址和端口号
                DatagramPacket packet = new DatagramPacket(
                        data,
                        data.length,
                        InetAddress.getByName("127.0.0.1"),
                        10010
                );
                socket.send(packet);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

